The JAVA Language provides event-handling mechanisms for detecting and responding to user actions performed relative to a user interface. Using these event-handling mechanisms, a programmer can specify operations that a program should perform in response to the occurrence of an event of a specified type relative to a specified element of a user interface. When an event of the specified type occurs, the program automatically invokes a specified set of instructions. The programmer is spared from the burden of coding instructions that periodically poll for the occurrence of events of the specified type.
Different programming languages provide different syntactical techniques for specifying event-handling mechanisms. In JAVA, the event-handling mechanisms traditionally have been coded using “EventListener” interfaces. In JAVA, an “interface” is a type, just as a class is a type. Like a class, an interface defines methods. Unlike a class, an interface never implements methods; instead, classes that implement the interface implement the methods defined by the interface. A class can implement multiple interfaces.
Historically, in order to utilize the functionality of an EventListener interface, a programmer needed to define a new class that implemented a specific one of the EventListener interfaces. Each EventListener interface may define a different set of methods, where each method corresponds to a different event type. For example, one method might correspond to a “mouse click” event type and another method might correspond to a “mouse over” event type. If a programmer wanted his code to perform a particular set of operations in response to an occurrence of the “mouse click” event type, the programmer needed to “implement” the appropriate method of the appropriate interface by placing a specific implementation of the method in the new class. Within the code of the implementation of the method, the programmer placed the instructions that would be executed upon the occurrence of the event of the event type corresponding to the method.
For example, a JAVA program might comprise code such as:                public class Foo {                    private JButton fred=new JButton(“fred”);            public class FredClicked implements ActionListener {                            public void actionPerformed(ActionEvent evt) {                                    System.out.println(“fred clicked”);                                                }                                    }            public void initialize( ) {                            fred.addActionListener(new FredClicked( ));                                    }                        }        
In the above code, the programmer-defined class “FredClicked” implements an EventListener interface called “ActionListener.” ActionListener defines the “actionPerformed” method, which the programmer has implemented within FredClicked. The implementation of the method causes the text “fred clicked” to be sent to the system output device. Other classes that implement ActionListener may implement the “actionPerformed” method in different ways.
Often, a user interface contains multiple user interface elements (e.g., buttons, text boxes, radio buttons, checkboxes, etc.) with which a user can interact. Multiple different events can occur relative to each of these user interface elements. Using the traditional approach described above, a new class (similar in structure to the FredClicked class of the above example) needed to be defined for each different event type-user element pair. For example, if a user interface consisted of five user interface elements, and if there were five types of events that could occur relative to each of those user interface elements, and if a programmer wanted his program to perform specific operations in response to each possible event occurrence relative to each user interface element, then the programmer needed to implement twenty-five separate classes.
Over time, programmers often find it tedious to re-enter, again and again, new class definitions that are highly similar and essentially necessary only because of the syntactical requirements of the JAVA Language. However, because EventListener interfaces have been a central part of JAVA for so long, a drastic departure from the current interface-based way of specifying event-handling mechanisms might not fit well into the general scheme of JAVA. A radical change in programming semantics, to adopt significantly different techniques that are used by other programming languages, might not be well received by those who have become accustomed to JAVA and its characteristics—especially the JAVA type system.
Therefore, a technique that allowed a programmer to specify event-handling mechanisms in JAVA using more concise, less verbose syntax would be both useful and desirable.